package com.vegetable.modules.service.inbound.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vegetable.common.utils.R;
import com.vegetable.modules.entity.inbound.AgentDeliveryReturn;
import com.vegetable.modules.entity.inbound.AgentDeliveryReturnDetail;
import com.vegetable.modules.entity.inbound.po.AgentDeliveryReturnPO;
import com.vegetable.modules.mapper.inbound.AgentDeliveryReturnDetailMapper;
import com.vegetable.modules.mapper.inbound.AgentDeliveryReturnMapper;
import com.vegetable.modules.service.inbound.IAgentDeliveryReturnService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 入库-代办交货退货 服务实现类
 * </p>
 *
 * @author wangruijie
 * @since 2024-03-07
 */
@Slf4j
@Service
public class AgentDeliveryReturnServiceImpl extends ServiceImpl<AgentDeliveryReturnMapper, AgentDeliveryReturn> implements IAgentDeliveryReturnService {

    @Resource
    private AgentDeliveryReturnDetailMapper agentDeliveryReturnDetailMapper;
    
    @Resource
    private AgentDeliveryReturnMapper agentDeliveryReturnMapper;

    @Override
    public List<AgentDeliveryReturn> getReturnsByDeliveryId(Long deliveryId) {
        return baseMapper.selectList(
            new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<AgentDeliveryReturn>()
                .eq("delivery_id", deliveryId)
        );
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R saveReturn(AgentDeliveryReturn agentDeliveryReturn) throws Exception {
        try {
            // 设置创建时间
            if (agentDeliveryReturn.getId() == null) {
                agentDeliveryReturn.setCreateTime(new Date());
            }
            
            // 保存退货主表
            this.saveOrUpdate(agentDeliveryReturn);
            
            // 删除原有的退货明细
            if (agentDeliveryReturn.getId() != null) {
                agentDeliveryReturnDetailMapper.delete(
                    new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<AgentDeliveryReturnDetail>()
                        .eq("return_id", agentDeliveryReturn.getId())
                );
            }
            
            // 保存退货明细
            if (agentDeliveryReturn.getReturnDetails() != null && !agentDeliveryReturn.getReturnDetails().isEmpty()) {
                for (AgentDeliveryReturnDetail detail : agentDeliveryReturn.getReturnDetails()) {
                    detail.setId(null); // 确保是新增
                    detail.setReturnId(agentDeliveryReturn.getId());
                    agentDeliveryReturnDetailMapper.insert(detail);
                }
            }
            
            return R.data(agentDeliveryReturn);
        } catch (Exception e) {
            log.error("保存退货信息失败", e);
            throw new Exception("保存退货信息失败: " + e.getMessage());
        }
    }

    @Override
    public R getReturnDetail(Long id) {
        AgentDeliveryReturn returnInfo = baseMapper.selectById(id);
        if (returnInfo != null) {
            // 查询退货明细
            List<AgentDeliveryReturnDetail> details = agentDeliveryReturnDetailMapper.selectList(
                new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<AgentDeliveryReturnDetail>()
                    .eq("return_id", id));
            returnInfo.setReturnDetails(details);
        }
        return R.data(returnInfo);
    }

    @Override
    public R queryPage(AgentDeliveryReturnPO po) {
        try {
            // 创建分页对象
            IPage<AgentDeliveryReturn> page = new Page<>(po.getPage(), po.getLimit());
            // 执行分页查询
            IPage<AgentDeliveryReturn> result = agentDeliveryReturnMapper.queryPage(page, po);
            return R.data(result);
        } catch (Exception e) {
            log.error("分页查询退货记录失败", e);
            return R.fail("分页查询退货记录失败: " + e.getMessage());
        }
    }
}